from flask import Blueprint,render_template,request,redirect,session
from demo1.utils import DbConfigUtils

ac=Blueprint('account',__name__)

@ac.route('/login',methods=['GET','POST'])
def login():
    if request.method=='GET':
        return render_template("login.html")
    #wtforms 表单验证
    role=request.form.get('role')
    mobile=request.form.get('mobile')
    password=request.form.get('password')
    print(role,mobile,password)
    # 连接mysql 查询用户名密码是否正确
    result=DbConfigUtils.fetchone("select * from user where mobile=%s and password=%s and role=%s",(mobile,password,role) )
    print(result)
    if result:
        # 保存session
        session['user_info']={'role':result['role'],'real_name':result['real_name'],'id':result['id']}
        return redirect("/order/list")
    return render_template("login.html",error="用户名或密码错误")


@ac.route('/users')
def users():
    return '用户列表'

@ac.route('/main')
def main():
    return render_template("main.html")